package com.ronsai.umtrack.presenter;

import android.content.Context;

import com.ronsai.umtrack.MyApp;
import com.ronsai.umtrack.api.ObserverResponseListener;
import com.ronsai.umtrack.contract.LoginContract;
import com.ronsai.umtrack.model.LoginModel;
import com.ronsai.umtrack.util.SPUtils;
import com.ronsai.umtrack.util.ToastUtil;

import java.util.HashMap;

public class LoginPresenter extends LoginContract.Presenter {

    private LoginModel<String> loginModel;
    private Context mContext;

    public LoginPresenter(Context context) {
        loginModel = new LoginModel<>();
        mContext = context;
    }

    @Override
    public void login(HashMap<String, String> map) {
        loginModel.login(mContext, map, new ObserverResponseListener<String>() {
            @Override
            public void onSuccess(String s) {
                SPUtils.getInstance().put("token", s);
                MyApp.setToken(s);
                if (getView() != null) {
                    getView().loginSuccess();
                }
                // 获取聊天token
//                HashMap<String, String> rongTokenMap = new HashMap<>();
//                rongTokenMap.put("token", s);
//                getRongToken(rongTokenMap);
            }

            @Override
            public void onError(int code, String msg) {
                ToastUtil.showShortToast(msg);
            }
        });
    }

    @Override
    public void getRongToken(HashMap<String, String> map) {
        loginModel.getRongToken(mContext, map, new ObserverResponseListener<String>() {
            @Override
            public void onSuccess(String s) {
                SPUtils.getInstance().put("rongToken", s);
                if (getView() != null) {
                    getView().loginSuccess();
                }
            }

            @Override
            public void onError(int code, String msg) {
                ToastUtil.showShortToast(msg);
            }
        });
    }
}
